#include "stdafx.h"
#include "win/win_imglist.h"

img_list::img_list()
{
}


img_list::~img_list()
{
    release();
}

void img_list::release()
{
    for(std::vector<HBITMAP>::iterator it = _hbmlist.begin(); it != _hbmlist.end(); ++it)
    {
        DeleteObject(*it);
    }
    _hbmlist.clear();
    _hbmindex.clear();
}


int img_list::load(std::string name, UINT id, HINSTANCE inst)
{
    HBITMAP hbm = ::LoadBitmap(inst, MAKEINTRESOURCE(id));
    if(!hbm)
    {
        return -1;
    }

    _hbmlist.push_back(hbm);
    _hbmindex[name] = _hbmlist.size() - 1;

    return 0;
}

HBITMAP img_list::get(int idx)
{
    if(idx < 0 || idx >= (int)_hbmlist.size())
    {
        return NULL;
    }
    return _hbmlist[idx];
}

HBITMAP img_list::get(std::string name)
{
    std::map<std::string, int>::iterator it = _hbmindex.find(name);
    if(_hbmindex.end() == it)
    {
        return NULL;
    }
    return get(it->second);
}